import { ref } from "vue";
import { defineStore } from "pinia";
import { useCartStore } from "@/stores/cartStore.js";
import {
  getheadapi,
  getbannerapi,
  getnewsapi,
  gethotapi,
  getgoodsapi,
  getcategroyapi,
  getsubcategoryapi,
  getloginapi,
} from "@/apis/layout.js";
export const useCategroyStore = defineStore(
  "categroy",
  () => {
    const userStore = useCartStore();
    const categorylist = ref([]);
    const bannerList = ref([]);
    const newsList = ref([]);
    const hotList = ref([]);
    const goodsList = ref([]);
    const categroysecList = ref([]);
    const subcategroyList = ref([]);
    const loginList = ref({});

    const getheadcategory = async () => {
      const res = await getheadapi();
      categorylist.value = res.data.result;
    };
    const getbanner = async (params = {}) => {
      const { distributionSite } = params;
      const res = await getbannerapi(distributionSite);
      bannerList.value = res.data.result;
    };
    const getnew = async () => {
      const res = await getnewsapi();
      newsList.value = res.data.result;
    };
    const gethot = async () => {
      const res = await gethotapi();
      hotList.value = res.data.result;
    };
    const getgoods = async () => {
      const res = await getgoodsapi();
      goodsList.value = res.data.result;
    };
    const getcategroysecList = async (id) => {
      const res = await getcategroyapi(id);
      categroysecList.value = res.data.result;
    };
    const getsubcategroy = async (id) => {
      const res = await getsubcategoryapi(id);
      subcategroyList.value = res.data.result;
    };
    const getlogin = async (params) => {
      const res = await getloginapi(params);
      loginList.value = res.data.result;
    };
    const clearlogin = () => {
      loginList.value = {};
      userStore.clearallcart();
    };

    return {
      categorylist,
      bannerList,
      newsList,
      hotList,
      goodsList,
      categroysecList,
      subcategroyList,
      loginList,
      getheadcategory,
      getbanner,
      getnew,
      gethot,
      getgoods,
      getcategroysecList,
      getsubcategroy,
      getlogin,
      clearlogin,
    };
  },
  {
    persist: {
      paths: ["loginList"],
    },
  }
);
